package com.meiyu.live.widget;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.meiyu.live.R;

import java.util.HashMap;
import java.util.Map;


/**
 * 分享对话框
 */
public class ShareDialog extends Dialog implements View.OnClickListener {

    private Activity context;

    private View btn_cancel;
    private String content = "http://fir.im/maitotv";
    private String description = "活动";
    private String title = "魅鱼直播";
    private String imgUrl;
    private Bitmap img;

    private LinearLayout layout_wechat_circle, layout_wechat, layout_qq, layout_qzone, layout_sina;

    Map<Integer, SHARE_MEDIA> share_mediaMap = new HashMap<Integer, SHARE_MEDIA>();


    public ShareDialog(Activity context, String title, String description, String content) {
        super(context, R.style.dialog_custom);
        this.title = title;
        this.description = description;
        this.content = content;
        init(context);
    }

    public ShareDialog(Activity context, String title, String description, String imgUrl, String content) {
        this(context, title, description, content);
        this.imgUrl = imgUrl;
    }

    public ShareDialog(Activity context, String title, String description, Bitmap img, String content) {
        this(context, title, description, content);
        this.img = img;
    }

    private void init(Activity context) {
        this.context = context;
        setContentView(R.layout.dialog_share);
        setCanceledOnTouchOutside(false);

        btn_cancel = findViewById(R.id.btn_cancel);

        share_mediaMap.put(R.id.layout_qq, SHARE_MEDIA.QQ);
        share_mediaMap.put(R.id.layout_wechat, SHARE_MEDIA.WEIXIN);
        share_mediaMap.put(R.id.layout_wechat_circle, SHARE_MEDIA.WEIXIN_CIRCLE);
        share_mediaMap.put(R.id.layout_sina, SHARE_MEDIA.SINA);
        share_mediaMap.put(R.id.layout_qzone, SHARE_MEDIA.QZONE);
        btn_cancel.setOnClickListener(this);
        layout_wechat_circle = (LinearLayout) findViewById(R.id.layout_wechat_circle);
        layout_wechat = (LinearLayout) findViewById(R.id.layout_wechat);
        layout_qq = (LinearLayout) findViewById(R.id.layout_qq);
        layout_qzone = (LinearLayout) findViewById(R.id.layout_qzone);
        layout_sina = (LinearLayout) findViewById(R.id.layout_sina);
        layout_wechat_circle.setOnClickListener(this);
        layout_wechat.setOnClickListener(this);
        layout_qq.setOnClickListener(this);
        layout_qzone.setOnClickListener(this);
        layout_sina.setOnClickListener(this);

    }

    UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(context, "分享成功", Toast.LENGTH_SHORT).show();
            dismiss();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(context, "分享失败", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
//            Toast.makeText(context, "分享取消", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_wechat:
            case R.id.layout_wechat_circle:
            case R.id.layout_qq:
            case R.id.layout_qzone:
            case R.id.layout_sina:
                UMImage image;
                if (TextUtils.isEmpty(imgUrl)) {
                    image = new UMImage(context, img);
                } else {
                    image = new UMImage(context, imgUrl);
                }
                new ShareAction(context)
                        .setPlatform(share_mediaMap.get(view.getId()))
                        .setCallback(umShareListener)
                        .withTitle(title)
                        .withText(description)
                        .withTargetUrl(content)
                        .withMedia(image)
                        .share();
            case R.id.btn_cancel:
                dismiss();
                break;
        }
    }
}
